import 'package:just_audio/just_audio.dart';

class AudioPlayerManager {
  final AudioPlayer _player = AudioPlayer();
  List<String> _assetPaths = [];

  /// 设置音频列表（assets 路径），并自动播放第一个
  Future<void> setAudioList(
    List<String> assetPaths, {
    double speed = 1.0, // 默认正常速度
    double volume = 1.0, // 默认最大音量
  }) async {
    if (assetPaths.isEmpty) return;

    stop();
    _assetPaths = List<String>.from(assetPaths);

    final audioSources = _assetPaths.map((path) => AudioSource.uri(Uri.parse('asset:///$path'))).toList();

    await _player.setAudioSources(audioSources);

    // 设置速度和音量
    await _player.setSpeed(speed);
    await _player.setVolume(volume);

    await _player.play();
  }

  /// 动态调整播放速度（0.5 - 2.0 通常是合理范围）
  Future<void> setSpeed(double speed) async {
    await _player.setSpeed(speed);
  }

  /// 动态调整音量（0.0 - 1.0）
  Future<void> setVolume(double volume) async {
    await _player.setVolume(volume);
  }

  /// 获取当前速度
  double get speed => _player.speed;

  /// 获取当前音量
  double get volume => _player.volume;

  /// 停止播放并清空列表
  Future<void> stop() async {
    await _player.stop();
    _assetPaths.clear();
  }

  /// 释放资源
  Future<void> dispose() async {
    await stop();
    await _player.dispose();
  }
}
